package com.njlime.cms.controller.app;

import com.njlime.cms.entities.Community;
import com.njlime.cms.entities.CommunityComment;
import com.njlime.cms.entities.CommunityCommentUp;
import com.njlime.cms.entities.CommunityUp;
import com.njlime.cms.service.app.AppCommunityCommentService;
import com.njlime.cms.service.app.AppCommunityCommentUpService;
import com.njlime.cms.service.app.AppCommunityService;
import com.njlime.cms.service.app.AppCommunityUpService;
import com.njlime.core.base.controller.BaseAppController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.AppWxInterceptor;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.app.AppMemberTrackService;
import com.njlime.member.service.app.AppPublishReportService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping(value = "/app/community")
public class AppCommunityController extends BaseAppController {
    private final Logger logger = Logger.getLogger(AppCommunityController.class);
    @Resource
    private AppCommunityService appCommunityService;
    @Resource
    private AppCommunityUpService appCommunityUpService;
    @Resource
    private AppCommunityCommentUpService appCommunityCommentUpService;
    @Resource
    private AppMemberTrackService appMemberTrackService;
    @Resource
    private AppPublishReportService appPublishReportService;
    @Resource
    private AppCommunityCommentService appCommunityCommentService;

    /**
     * 获取社区列表
     *
     * @param pageBean
     * @param community
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public String list(PageBean pageBean, Community community, HttpServletRequest request, Integer type) {
        logger.info("/app/community/list");
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        Integer root_unit_id = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ROOT_UNIT_ID);
        community.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        community.setRoot_unit_id(root_unit_id);
        return toJsonListData(appCommunityService.list(pageBean, community, uid));
    }

    /**
     * 获取社区评论列表
     */
    @ResponseBody
    @RequestMapping(value = "/commentList")
    public String commentList(HttpServletRequest request, PageBean pageBean, CommunityComment communityComment) {
        logger.info("/app/community/commentList");
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        return toJsonListData(appCommunityService.commentList(pageBean, communityComment, uid));
    }

    /**
     * 获取社区及评论列表
     */
    @ResponseBody
    @RequestMapping(value = "/detail")
    public String detail(HttpServletRequest request, Integer id) {
        logger.info("/app/community/detail");
        Integer unit_id = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_UNIT_ID);
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        if (uid != null) {
            appMemberTrackService.save(uid, GlobalValue.BusinessType.COMMUNITY, id);
        }
        Map<String, Object> bean = appCommunityService.get(id, null, uid);
        Integer business_unit = Integer.valueOf(bean.get("unit_id").toString());
        appPublishReportService.save(GlobalValue.BusinessType.COMMUNITY, id, unit_id, business_unit);
        return toJsonSuccessData(bean);
    }

    /**
     * 获取社区中奖列表
     */
    @ResponseBody
    @RequestMapping(value = "/rewardList")
    public String rewardList(HttpServletRequest request, PageBean pageBean, Integer id) {
        logger.info("/app/community/rewardList");
        return toJsonListData(appCommunityCommentService.rewardList(pageBean, id));
    }

    /**
     * 添加评论
     *
     * @param request
     * @param communityComment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/commentMake")
    public String commentMake(HttpServletRequest request, CommunityComment communityComment) {
        logger.info("/app/community/commentMake");
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        String name = (String) request.getSession().getAttribute(AppWxInterceptor.WX_USER_NAME);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "请登录后添加评论");
            } else {
                Community community = appCommunityService.findOne(communityComment.getCommunity_id());
                communityComment.setUser_id(uid);
                communityComment.setCreator(uid);
                communityComment.setUser_name(name);
                communityComment.setFabulous_num(0);
                communityComment.setCreate_time(new Date());
                communityComment.setIs_top(GlobalValue.NO);
                communityComment.setIs_del(GlobalValue.NO);
                communityComment.setIs_reward(GlobalValue.NO);
                if(community.getIs_comment() == GlobalValue.YES){
                    communityComment.setStatus(GlobalValue.UNAUDIT);
                    appCommunityService.addComment(communityComment, null);
                    return toJsonErrorData(ExceptionState.NEED_AUDIT_EXCEPTION,"添加成功，请等待审核");
                }else{
                    communityComment.setStatus(GlobalValue.AUDITED);
                    //用户评论
                    MemberComment memberComment = new MemberComment();
                    memberComment.setMember_id(communityComment.getUser_id());
                    memberComment.setBusiness_id(communityComment.getCommunity_id());
                    memberComment.setBusiness_type(GlobalValue.BusinessType.COMMUNITY);
                    memberComment.setBusiness_title(community.getTitle());
                    memberComment.setCreate_time(new Date());
                    appCommunityService.addComment(communityComment, memberComment);
                }
                return toJsonSuccessData();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "评论添加失败");
        }
    }

    /**
     * 点赞
     */
    @ResponseBody
    @RequestMapping(value = "infoUpAdd")
    public String infoUpAdd(HttpServletRequest request, CommunityUp communityUp) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "用户未登录");
            } else {
                if (appCommunityUpService.isUped(communityUp.getCommunity_id(), uid)) {
                    return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "请勿重复点赞");
                } else {
                    communityUp.setUp_user_id(uid);
                    communityUp.setCreate_time(new Timestamp(System.currentTimeMillis()));
                    appCommunityService.addUp(communityUp);
                    return toJsonSuccessData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "点赞失败");
        }
    }

    /**
     * 评论点赞
     */
    @ResponseBody
    @RequestMapping(value = "commentUpAdd")
    public String commentUpAdd(HttpServletRequest request, CommunityCommentUp communityCommentUp) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "用户未登录");
            } else {
                if (appCommunityCommentUpService.isUped(communityCommentUp.getComment_id(), uid)) {
                    appCommunityCommentUpService.del(communityCommentUp.getComment_id(), uid);
                    return toJsonSuccessData();
                } else {
                    communityCommentUp.setUp_user_id(uid);
                    communityCommentUp.setCreate_time(new Timestamp(System.currentTimeMillis()));
                    appCommunityCommentUpService.save(communityCommentUp);
                    return toJsonSuccessData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "点赞失败");
        }
    }
}
